//
//  ZZCReadViewController.m
//  B段项目
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "ZZCReadViewController.h"
#import "ZZCReadModel.h"
#import "ZZCHeadCollectionReusableView.h"
#import "ZZCReadCollectionViewCell.h"
#import "ZZCFootCollectionReusableView.h"
#import "ZZCReadDetailViewController.h"
#import "ZZCWebViewController.h"
@interface ZZCReadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, ZZCReadViewDelegate>

@property(nonatomic, retain)UICollectionView *readCollectionView;/**<阅读 collectionView*/
@property(nonatomic, retain)NSMutableArray *allArray;/**<阅读 数组*/
@property(nonatomic, retain)NSMutableArray *showArray;/**<阅读 轮播图数组*/

@end

@implementation ZZCReadViewController

-(void)viewWillAppear:(BOOL)animated{
    
    
    [self.readCollectionView reloadData];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLabel.text = @"阅读";
    
    //图标
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - WIDTH/4.5, WIDTH/40, WIDTH/16, WIDTH/16)];
    img.image = [UIImage imageNamed:@"iconfont-shu2.png"];
    img.center = CGPointMake(WIDTH - WIDTH/5, self.navigationController.navigationBar.frame.size.height /2);
    [self.rightView addSubview:img];
    
    //请求数据
    [self requestData];
    
#pragma mark 布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //每个section
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //最小行间距
    flowLayout.minimumLineSpacing = 5;
    //最小列间距
    flowLayout.minimumInteritemSpacing = 5;
    
    
#pragma mark 设置collectionView
    self.readCollectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    self.readCollectionView.delegate = self;
    self.readCollectionView.dataSource = self;
    self.readCollectionView.backgroundColor = [UIColor whiteColor];//如果不设置 默认为黑色
    [self.view addSubview:self.readCollectionView];
    //取消滚动条
    self.readCollectionView.showsVerticalScrollIndicator = NO;
    
#pragma mark 注册
    
    //smallCell
    [self.readCollectionView registerClass:[ZZCReadCollectionViewCell class] forCellWithReuseIdentifier:@"_small"];
    //bigCell
    [self.readCollectionView registerClass:[ZZCReadCollectionViewCell class] forCellWithReuseIdentifier:@"_big"];
    
    //Header    注意: 注册方法是不同的!!!!
    //UICollectionElementKindSectionHeader 注册的是头视图
    [self.readCollectionView registerClass:[ZZCHeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    //footer
    [self.readCollectionView registerClass:[ZZCFootCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
    
    //初始化 数组
    self.allArray = [NSMutableArray array];
    self.showArray = [NSMutableArray array];
    
    
}

#pragma mark - 请求数据
-(void)requestData{
    
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"正在为您加载.....";
    [self.view addSubview:hub];
    
    NSString *urlStr = @"http://api2.pianke.me/read/columns";
    NSString *body = @"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&version=3.0.6";
    
    [ZYTools postUrl:urlStr body:body result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        
        
        NSMutableDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *array = [dic objectForKey:@"list"];
        //把轮播图的数组取出来放数组里
        self.showArray = [dic objectForKey:@"carousel"];
        
        for (NSMutableDictionary *smallDic in array) {
            ZZCReadModel *readMod = [[ZZCReadModel alloc]init];
            [readMod setValuesForKeysWithDictionary:smallDic];
            [self.allArray addObject:readMod];
        }
        
        [self.readCollectionView reloadData];
        
        [hub hide:YES];
        
    } failure:^(NSError *error) {
        
        [hub hide:YES];
    }];
    
}

#pragma mark - 设置有几个区
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

#pragma mark - 设置每个区有几个item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.allArray.count;
}

#pragma mark - 设置item内容  赋值
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZZCReadCollectionViewCell *smallCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"_small" forIndexPath:indexPath];
    
    smallCell.readList = [self.allArray objectAtIndex:indexPath.row];
    
    return smallCell;
}

#pragma mark - 设置每个item 大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((WIDTH - 30) / 3, (WIDTH - 30) / 3);
}

#pragma mark - 设置sectionHeader的大小  若想显示Header 必须返回Header高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    
    if (section == 0) {
        return CGSizeMake(WIDTH, (HEIGHT - 64) / 3);
    }else{
        return CGSizeZero;
    }
    
}

#pragma mark - 设置sectionFooter的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
    if (section == 0) {
        return CGSizeMake(WIDTH, (HEIGHT - 64) / 4);
    }else{
        return CGSizeZero;
    }
}

#pragma mark - 设置头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    //如果要创建的是头视图时
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        ZZCHeadCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        header.array = self.showArray;
        header.readHeadDelegate = self;
        return header;
    }else if (kind == UICollectionElementKindSectionFooter){
        ZZCFootCollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
        
        footer.footerView.image = [UIImage imageNamed:@"dishitu.png"];
        
        return footer;
    }
    
    
    return nil;
    
    
}

#pragma mark - 触摸cell时触发的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZZCReadDetailViewController *detailVC = [[ZZCReadDetailViewController alloc]init];
    
    //把model传给二级界面大controller中
    detailVC.DetailModel = [self.allArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(void)presentToViewControllerContentId:(NSString *)contentId
{
    ZZCWebViewController *detailVC = [[ZZCWebViewController alloc] init];
                                                                                                                                                          detailVC.contentId = contentId;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
